<template>
	<view class="slider-verify" :style="'background-color:'+sBgColor" @touchend="touchend">
		<view class="slider-prompt" :style="success?'color:'+sColor:'color:'+sBgColor"> 电话报名</view>
		<view class="slider-bg" :style="{'transform':'translateX('+oldx+'px)',backgroundColor:dBgColor}">
			<view class="big-bg">
				<view class="bg">
				</view>
			</view>
		</view>
		<movable-area class="slider-area " :animation="true">
			<movable-view :style="{borderColor:success?sBgColor:dBgColor}" :inertia='true' damping='30'
				:class="{'movable-btn':true,'movable-success':success }" :x="x" direction="horizontal" @change="onMove"
				:disabled="isDisable">
			</movable-view>
		</movable-area>
	</view>
</template>
<script>
	export default {
		props: {
			//偏移量
			offset: {
				type: Number,
				default: 10
			},
			//滑动轨道默认背景色
			dBgColor: {
				type: String,
				default: '#f0f0f0'
			},
			//滑动轨道滑过背景色
			sBgColor: {
				type: String,
				default: '#4E9900'
			},
			//默认文字颜色
			dColor: {
				type: String,
				default: '#4E9900'
			},
			//成功文字颜色
			sColor: {
				type: String,
				default: '#FFFFFF'
			},
		},
		data() {
			return {
				x: 0,
				oldx: 0,
				success: false, //是否验证成功
				verification: 0, //验证次数
				isDisable: this.disabled,
				screenWidth: 0,
				isMove: false
			};
		},
		mounted() {
			const systemInfo = uni.getSystemInfoSync()
			this.screenWidth = systemInfo.screenWidth
		},
		methods: {
			onMove(e) {
				this.isMove = true
				this.oldx = e.detail.x
			},
			touchend() {
				if (this.success || (this.oldx < 1 && this.oldx != 0.1)) return
				this.x = this.oldx
				var promptW = 0
				var onTrackW = 0
				uni.createSelectorQuery().in(this).select(".slider-prompt").boundingClientRect(data => {
					if (data.width > 0) {
						promptW = data.width
						uni.createSelectorQuery().in(this).select(".movable-btn").boundingClientRect(data => {
							if (data.width > 0) {
								onTrackW = data.width
								if (this.oldx != 0.1) this.verification++
								if (this.oldx > (promptW - onTrackW - this.offset)) {
									this.success = true
									this.$emit('onPlay')
									this.isDisable = true
									this.verificationSuccess(true)
								} else {
									this.$nextTick(() => {
										this.x = 0
										this.oldx = 0
									})
									this.verificationSuccess(false)
								}
							}
						}).exec()
					}
				}).exec()
			},
			verificationSuccess(state) {
				let obj = {
					state: state,
				}
				this.$emit("change", obj)
			},
			//重置初始化状态
			initialization() {
				this.$nextTick(() => {
					this.x = 0
					this.oldx = 0.1
					this.success = false
					this.isDisable = false
					this.touchend()
				})
			}
		}
	}
</script>
<style scoped>
	.slider-verify {
		position: relative;
		width: 694rpx;
		height: 102rpx;
		overflow: hidden;
		border-radius: 50rpx;
		background: #4E9900;
		box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(78, 153, 0, 0.3);
		border: 1rpx solid #4E9900;
	}

	.slider-prompt {
		border-radius: 50rpx;
		width: 100%;
		height: 100%;
		position: absolute;
		left: 0;
		top: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		z-index: 99;
		border-radius: 0 0 50rpx 50rpx;
	}

	.slider-bg {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: -1rpx;
	}

	.slider-area {
		position: absolute;
		top: 0;
		left: 0;
		height: 100%;
		width: 100%;
		z-index: 999;
	}

	.issuccess {
		background: #4E9900;
	}

	.issuccess1 {
		background: rgba(255, 255, 255, 0.86);
	}

	.movable-btn {
		width: 104rpx;
		height: 104rpx;
	}

	.bg {
		background: #4E9900 url(https://images.linglinggong.net/static/go-left.png);
		width: 104rpx;
		height: 104rpx;
		box-sizing: border-box;
		background-position: left;
		background-size: contain;
		background-repeat: no-repeat;
		border-radius: 0 100% 100% 0;
	}

	.movable-success {
		background-image: url(https://images.linglinggong.net/static/go-left.png);
		background-size: cover;
	}
</style>